#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char const *argv[])
{
        int num = 10;
        pid_t pid;
        int i = 0;

        for (i = 0; i < num; i++){
                pid = fork();
                if(pid == 0){
                        printf("I am child,pid=%d, ppid=%d\n",getpid(),getppid());
                        break;
                }
        }
        sleep(i);

        if(i == num){
                pid_t wpid;
                int status;
                while ((wpid = waitpid(-1, &status, WNOHANG)) != -1){
                        if(wpid == 0){ //子进程正在运行
                                continue;
                        }

                        printf("回收的子进程ID号 wpid = %d\n", wpid);
                        if(WIFEXITED(status)){
                                printf("child exit with %d\n", WEXITSTATUS(status));
                        }
                        if(WIFSIGNALED(status)){
                                printf("child killed by %d\n", WTERMSIG(status));
                        }
                }
        }
        return 0;
}
